package com.dayaokeji.rhythmschoolstudent.client.home.course

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.text.TextUtils
import android.view.Menu
import android.view.MenuItem
import android.view.View
import com.dayaokeji.rhythmschoolstudent.R
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity
import com.dayaokeji.rhythmschoolstudent.client.home.course.adapter.CourseTableViewPagerAdapter
import com.dayaokeji.rhythmschoolstudent.client.home.join.JoinMeetingAndCourseActivity
import com.dayaokeji.rhythmschoolstudent.client.mine.CourseSearchActivity
import com.dayaokeji.rhythmschoolstudent.utils.CourseTableUtil
import com.dayaokeji.rhythmschoolstudent.utils.DateUtils
import com.dayaokeji.rhythmschoolstudent.utils.PrefUtils
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils
import jahirfiquitiva.libs.fabsmenu.FABsMenu
import jahirfiquitiva.libs.fabsmenu.FABsMenuListener
import kotlinx.android.synthetic.main.activity_course_table.*
import java.util.*

/**
 * 课程表
 */
class CourseTableActivity : BaseActivity() {

    companion object {

        private const val COURSE_FRAGMENT_TAG = "course_fragment_tag"

        fun navigation(context: Context) {
            val intent = Intent(context, CourseTableActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_course_table)
        toolbar?.let {
            setSupportActionBar(it)
        }

        init()
    }

    private fun init() {
        setupView()
    }

    private fun setupView() {
        val fragments: ArrayList<Fragment> = arrayListOf()
        val weeks = CourseTableUtil.getWeeks()

        val webDate = PrefUtils.getWebDate()
        val format = DateUtils.format(System.currentTimeMillis(), DateUtils.FORMAT_DATE)
        var currentDate = DateUtils.formatDate(format,DateUtils.FORMAT_DATE)
        if (!TextUtils.isEmpty(webDate)) {
            currentDate = DateUtils.formatDate(webDate,DateUtils.FORMAT_DATE)
        }

        var currentIndex = 0
        for ((index,value) in weeks.withIndex()) {
            val startDate = DateUtils.formatDate(value["start"], DateUtils.FORMAT_DATE)
            val endDate = DateUtils.formatDate(value["end"], DateUtils.FORMAT_DATE)
            if ( ((currentDate.after(startDate) || currentDate.equals(startDate))) && ((currentDate.before(endDate) || currentDate.equals(endDate)))){
                currentIndex = index
            }
        }
        for (week in weeks) {
            fragments.add(CourseTableFragment.newInstance(week["start"],week["end"]))
        }
        val adapter = CourseTableViewPagerAdapter(supportFragmentManager,fragments)
        vpCourseTable.adapter = adapter
        vpCourseTable.setCurrentItem(currentIndex,true)

        // 设置课程表+号按钮
        fabsMenu.menuButtonColor = resources.getColor(R.color.technology_blue)
        fabsMenu.menuButtonRippleColor = resources.getColor(R.color.technology_blue)
        fabsMenu.menuButtonIcon = resources.getDrawable(R.mipmap.ic_add)

        // 底部fabs按钮菜单事件
        addCourseAndMeeting.setOnClickListener {
            JoinMeetingAndCourseActivity.navigation(this,JoinMeetingAndCourseActivity.SearchType.COURSE)
        }

        searchCourse.setOnClickListener {
            CourseSearchActivity.navigation(this)
        }

        modifyCourseTable.setOnClickListener {
            CourseErrorHelperActivity.navigation(this)
        }

    }
}
